<template>
  <div>
    <form-marker 
      :isEdit="isEditPage"
      :formData="list" 
      :rulesList="rules"
      :selectList="selectData"
      :eidtList="detailData"
      v-on:on-address="onchangeAddress" 
      v-on:on-confirm="onSaveInfo" 
      v-on:on-cancel="onCancelInfo"></form-marker>
  </div>
</template>
<script>
import FormMarker from '@/components/createPage/createMarker'
import {isPhone} from '@/common/utils'
export default {
  components: {
    FormMarker
  },
  data () {
    // 自定义验证规则
    let onCheckCompanyName = (rule, value, callback) => {
      if (value && !this.noRepeat && this.pageType === 'new') {
        this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/checkCompanyOnly`, {companyName: value}).then((res) => {
          if (res.result) {
            this.noRepeat = res.data // res.data.data [true]有重复/【false】无重复
            if (!res.data) {
              callback(new Error('名称重复已存在，请重新输入'))
            }
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        callback()
      }
    }
    // 验证address
    let onCheckAddress = (rule, value, callback) => {
      if (this.isEditPage || (this.checkAddress && this.checkAddress.provinceName && this.checkAddress.cityName && this.checkAddress.countyName)) {
        callback()
      } else {
        callback(new Error('地址不允许为空'))
      }
    }
    // 验证联系电话
    let onCheckPhone = (rule, value, callback) => {
      if (!isPhone(value)) {
        callback(new Error('请输入正确格式的联系电话'))
      } else {
        callback()
      }
    }
    // 去除汉字
    let onCheckChina = (rule, value, callback) => {
      if (value && /^[\u4E00-\u9FA5]{0,10}$/.test(value)) {
        callback(new Error('请输入正确格式'))
      } else {
        callback()
      }
    }
    return {
      list: [
        {
          type: 'input',
          label: '公司名称',
          name: '公司名称',
          prop: 'name',
          isDisable: true
        },
        {
          type: 'selectProvince',
          label: '办公地址',
          name: '办公地址',
          prop: 'address',
          child: [
            {
              type: 'select',
              name: '省',
              prop: 'provinceName'
            },
            {
              type: 'select',
              name: '市',
              prop: 'cityName'
            },
            {
              type: 'select',
              name: '区县',
              prop: 'countyName'
            }
          ]
        },
        {
          type: 'input',
          name: '详细地址',
          prop: 'street'
        },
        {
          type: 'switch',
          label: '是否采购主体',
          prop: 'isProcuringEntity'
        },
        {
          type: 'select',
          label: '上级公司',
          name: '上级公司',
          prop: 'parentCode',
          propName: 'parentName',
          optionLabel: 'name',
          optionValue: 'code'
        },
        {
          type: 'select',
          label: '公司类型',
          name: '公司类型',
          prop: 'companyType',
          propName: 'companyTypeName',
          optionLabel: 'dicValue',
          optionValue: 'dicCode'
        },
        {
          type: 'select',
          label: '租户',
          name: '租户',
          prop: 'tenantId',
          propName: 'tenantName',
          optionLabel: 'name',
          optionValue: 'code'
        },
        {
          type: 'input',
          label: '开户银行',
          name: '开户银行',
          prop: 'depositBank'
        },
        {
          type: 'input',
          label: '银行账户',
          name: '开户银行',
          prop: 'depositAccoun'
        },
        {
          type: 'select',
          label: '纳税人识别类型',
          name: '纳税人识别类型',
          prop: 'taxpayerType',
          propName: 'taxpayerTypeName',
          optionLabel: 'dicValue',
          optionValue: 'dicCode'
        },
        {
          type: 'input',
          label: '纳税人识别号',
          name: '纳税人识别号',
          prop: 'taxpayerNumber'
        },
        {
          type: 'checkout',
          label: '是否与上级公司有结算关系',
          checkoutValue: '是',
          prop: 'shopHeaderAccountable'
        },
        {
          type: 'input',
          label: '联系人',
          name: '联系人',
          prop: 'contactsName'
        },
        {
          type: 'input',
          label: '联系人证件号',
          name: '联系人证件号',
          prop: 'contactsIdCard'
        },
        {
          type: 'input',
          label: '联系电话',
          name: '联系电话',
          prop: 'contactsPhone'
        },
        {
          type: 'shareArea',
          label: '共享方案组',
          name: '共享方案组',
          prop: 'shopHeaderAccountable'
        },
        {
          type: 'limitArea',
          label: '功能权限',
          prop: 'limitData'
        }
      ],
      rules: {
        name: [
          { max: 64, message: '长度不可超过64个字符', trigger: 'change' },
          { required: true, message: '公司名称不允许为空', trigger: 'change' },
          { required: true, validator: onCheckCompanyName, trigger: 'change' }
        ],
        province: [{ required: true, message: '省不允许为空', trigger: 'change' }],
        city: [{ required: true, message: '城市不允许为空', trigger: 'change' }],
        county: [{ required: true, message: '区县不允许为空', trigger: 'change' }],
        street: [
          { max: 128, message: '长度不可超过128个字符', trigger: 'blur' },
          { required: true, message: '详细地址不允许为空', trigger: 'blur' }
        ],
        contactsName: [
          { required: true, message: '联系人不允许为空', trigger: 'blur' },
          { max: 64, message: '长度不可超过64个字符', trigger: 'blur' }
        ],
        contactsPhone: [
          { max: 20, message: '长度不可超过20个字符', trigger: 'blur' },
          { required: true, message: '联系电话不允许为空', trigger: 'blur' },
          { validator: onCheckPhone, trigger: 'blur' }
        ],
        companyType: [
          { required: true, message: '公司类型不允许为空', trigger: 'change' }
        ],
        taxpayerType: [
          { required: true, message: '纳税人类型不允许为空', trigger: 'change' }
        ],
        depositBank: [
          { max: 64, message: '长度不可超过64个字符', trigger: 'blur' }
        ],
        depositAccoun: [
          { validator: onCheckChina, trigger: 'change' },
          { max: 64, message: '长度不可超过64个字符', trigger: 'change' }
        ],
        taxpayerNumber: [
          { validator: onCheckChina, trigger: 'change' },
          { max: 64, message: '长度不可超过64个字符', trigger: 'change' }
        ],
        contactsIdCard: [
          { validator: onCheckChina, trigger: 'change' },
          { max: 64, message: '长度不可超过64个字符', trigger: 'change' }
        ],
        address: [
          { required: true, validator: onCheckAddress, trigger: 'change' }
        ]
      },
      selectData: {},
      detailData: {},
      checkAddress: {},
      isEditPage: false,
      noRepeat: false // 编码唯一性
    }
  },
  mounted () {
    this.pageType = this.$route.params.id
    this.init()
  },
  methods: {
    init () {
      if (this.pageType === 'new') {
        this.getCompanySelect()
        this.getLimitList()
      }
      this.getCompanyTypeSelect()
      this.getTaxpayerTypeSelect()
      this.getTenantSelect()
      // this.getShareList()
      this.getCompanyDetail()
    },
    initDic () {
      let curCompanyType = this.$localStorage.get(this.$const.SAVEWEBINFO.COM)
      let curTaxpayerType = this.$localStorage.get(this.$const.SAVEWEBINFO.TAX)
      if (curCompanyType) {
        this.selectData.companyType = JSON.parse(JSON.stringify(curCompanyType))
      } else {
        this.getCompanyTypeSelect()
      }
      if (curTaxpayerType) {
        this.selectData.taxpayerType = JSON.parse(JSON.stringify(curTaxpayerType))
      } else {
        this.getTaxpayerTypeSelect()
      }
    },
    // 获取编辑详情数据
    async getCompanyDetail () {
      if (this.pageType !== 'new') {
        let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/getCompanyDetail`, {companyCode: this.pageType})
        this.detailData = data.company || {}
        this.detailData.shareSchemeGroups = data.shareSchemeGroups || []
        this.detailData.permissions = data.permissions || []
        this.isEditPage = true
        this.getEditCompanySelect()
      }
    },
    // 获取company下拉列表数据
    async getCompanySelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/findCompanyList`, {})
      this.selectData.parentCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取租户下拉列表数据
    async getTenantSelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/tenant/findTenantList`, {})
      this.selectData.tenantId = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取company下拉列表数据
    async getEditCompanySelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/findCompanyList`, {code: this.detailData.code || null})
      this.selectData.parentCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取companyType下拉列表数据
    async getCompanyTypeSelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/dictionary/findDictionaryByType`, {type: 'company_type'})
      this.selectData.companyType = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
      this.$localStorage.set(this.$const.SAVEWEBINFO.COM, this.selectData.companyType)
    },
    // 获取纳税人识别下拉列表数据
    async getTaxpayerTypeSelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/dictionary/findDictionaryByType`, {type: 'taxpayer_type'})
      this.selectData.taxpayerType = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
      this.$localStorage.set(this.$const.SAVEWEBINFO.TAX, this.selectData.taxpayerType)
    },
    // 获取共享方案组数据
    async getShareList () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/shareSchemeGroup/listShareSchemeGroup`, {})
      this.selectData.shopHeaderAccountable = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 权限树配置
    async getLimitList () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/findPermissionByCode`)
      this.selectData.limitData = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    onchangeAddress (val) {
      this.checkAddress = val
    },
    // 接口保存新建信息
    onSaveInfo (val, moreValue) {
      let params = val
      let operateType = this.pageType === 'new' ? 'save' : 'edit'
      params.isProcuringEntity = val.isProcuringEntity ? val.isProcuringEntity : false
      params.shopHeaderAccountable = params.shopHeaderAccountable ? params.shopHeaderAccountable : false
      if (params.permissions) delete params.permissions
      if (params.shareSchemeGroups) delete params.shareSchemeGroups
      this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/operationData`, {operateType: operateType, company: params, shareSchemeGroups: moreValue.shareSchemeGroups, permissionCodes: moreValue.permissionCodes}).then((res) => {
        if (res.result) {
          this.$message.success('操作成功')
          this.$router.push({name: 'company'})
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onCancelInfo () {
      this.$router.push({name: 'company'})
    }
  }
}
</script>
<style lang="scss" scoped>

</style>


